package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.LinkDao;
import com.itheima.pojo.Link;
import com.itheima.pojo.PictureSort;
import com.itheima.service.LinkService;
import com.itheima.utils.CookieUtils;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.LinkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加友情链接
     *
     * @param linkVO
     * @return
     */
    @Override
    public void add(LinkVO linkVO, HttpServletRequest request) {
        Link link = new Link();
        link.setUid(idWorker.nextId() + "");
        link.setTitle(linkVO.getTitle());
        link.setSummary(linkVO.getSummary());
        link.setUrl(linkVO.getUrl());
        link.setCreateTime(DateUtils.getNowTime());
        link.setUpdateTime(DateUtils.getNowTime());
        link.setStatus(1);
        link.setSort(0);
        Map<String, String> map = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            map.put(cookie.getName(), cookie.getValue());
        }
        String token = map.get("token");
        //判断cookie中是否包含uid
        if (token.contains("admin")) {
            String uid = CookieUtils.getUid(request, redisTemplate);
            link.setUid(uid);
        } else {
            String uid = CookieUtils.getUid(request, redisTemplate);
            link.setUserUid(uid);
        }
        linkDao.insert(link);
    }

    @Override
    public void delete(LinkVO linkVO, HttpServletRequest request) {
        Link link = new Link();
        link.setUid(linkVO.getUid());
        //逻辑删除
        link.setStatus(0);
        link.setLinkStatus(2);
        Map<String, String> map = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            map.put(cookie.getName(), cookie.getValue());
        }
        String token = map.get("token");
        //判断cookie中是否包含uid
        if (token.contains("admin")) {
            String uid = CookieUtils.getUid(request, redisTemplate);
            link.setAdminUid(uid);
        }
        linkDao.updateByPrimaryKeySelective(link);
    }

    /**
     * 编辑友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @Override
    public void edit(LinkVO linkVO, HttpServletRequest request) {
        Link link = linkDao.selectByPrimaryKey(linkVO.getUid());
        link.setTitle(linkVO.getTitle());
        link.setSummary(linkVO.getSummary());
        link.setUrl(linkVO.getUrl());
        link.setUpdateTime(DateUtils.getNowTime());
        link.setStatus(linkVO.getStatus());
        link.setLinkStatus(linkVO.getLinkStatus());
        Map<String, String> map = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            map.put(cookie.getName(), cookie.getValue());
        }
        String adminId = map.get("adminId");
        link.setAdminUid(adminId);
        linkDao.updateByPrimaryKeySelective(link);
    }

    /**
     * 查询友情链接
     *
     * @param linkVO
     * @return
     */
    @Override
    public Page<Link> getList(LinkVO linkVO) {
        //设置默认当前页
        if (linkVO.getCurrentPage() == null || "".equals(linkVO.getCurrentPage())) {
            linkVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (linkVO.getPageSize() == null || "".equals(linkVO.getPageSize())) {
            linkVO.setPageSize(10);
        }
        //当前页
        Integer currentPage = linkVO.getCurrentPage();
        //每页数据
        Integer pageSize = linkVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);
        //构造条件查询
        Example example = new Example(Link.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", 1);
        criteria.andEqualTo("linkStatus", 1);
        return (Page<Link>) linkDao.selectByExample(example);
    }

    /**
     * 置顶友情链接
     *
     * @param linkVO
     * @param request
     * @return
     */
    @Override
    public Page<Link> stick(LinkVO linkVO, HttpServletRequest request) {
        Link link = linkDao.selectByPrimaryKey(linkVO.getUid());
        link.setStatus(101);
        link.setUpdateTime(DateUtils.getNowTime());
        linkDao.updateByPrimaryKeySelective(link);
        return getList(linkVO);
    }

    //增加友情链接点击数
    @Override
    public void addLinkCount(String uid) {
        Link link=linkDao.selectByPrimaryKey(uid);
        if(link==null){
            throw new RuntimeException("这个链接不存在");
        }
        if(link.getClickCount()==null||link.getClickCount()<1){
            link.setClickCount(1);
        }else{
            link.setClickCount(link.getClickCount()+1);
        }
        linkDao.updateByPrimaryKey(link);
    }

    //获取友情链接
    @Override
    public List<Link> getLink() {
       Example example=new Example(Link.class);
       Example.Criteria criteria=example.createCriteria();
       criteria.andEqualTo("linkStatus",1);
       criteria.andEqualTo("status",1);
        List<Link> list = linkDao.selectByExample(example);
        return list;
    }

}
